﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using DevComponents.DotNetBar.Controls;
using System.Data;
using RM2012Server.Business;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class CustomerCtrl
    {
        CustomerDAO m_CustomerDAO = new CustomerDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "CusId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "CusId";
            cmb.DataPropertyName = "CusId";
            cmb.HeaderText = "Khách hàng";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "CusId";
            cmb.DataPropertyName = "CusId";
            cmb.HeaderText = "Khách hàng";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtCusId,
                         TextBoxX txtFullName,
                         CheckBoxX rbSex,
                         CheckBoxX rbMale,
                         CheckBoxX rbFemale,
                         TextBoxX txtDiscountPer,
                         TextBoxX txtDebt,
                         TextBoxX txtAddress,
                         TextBoxX txtPhone,
                         TextBoxX txtEmail,
                         TextBoxX txtState,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtCusId.DataBindings.Clear();
            txtCusId.DataBindings.Add("Text", bs, "CusId");

            txtFullName.DataBindings.Clear();
            txtFullName.DataBindings.Add("Text", bs, "FullName");

            rbSex.DataBindings.Clear();
            rbSex.DataBindings.Add("Checked", bs, "Sex");

            if (rbSex.Checked == true)
            {
                rbFemale.Checked = true;
                rbMale.Checked = false;
            }
            else
            {
                rbFemale.Checked = false;
                rbMale.Checked = true;
            }

            txtDiscountPer.DataBindings.Clear();
            txtDiscountPer.DataBindings.Add("Text", bs, "DiscountPer");

            txtDebt.DataBindings.Clear();
            txtDebt.DataBindings.Add("Text", bs, "Debt");

            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", bs, "Address");

            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", bs, "Phone");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bs, "Email");

            txtState.DataBindings.Clear();
            txtState.DataBindings.Add("Text", bs, "State");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }


        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtCusId,
                         TextBoxX txtFullName,
                         CheckBoxX rbSex,
                         CheckBoxX rbMale,
                         CheckBoxX rbFemale,
                         TextBoxX txtDiscountPer,
                         TextBoxX txtDebt,
                         TextBoxX txtAddress,
                         TextBoxX txtPhone,
                         TextBoxX txtEmail,
                         TextBoxX txtState,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtCusId.DataBindings.Clear();
            txtCusId.DataBindings.Add("Text", bs, "CusId");

            txtFullName.DataBindings.Clear();
            txtFullName.DataBindings.Add("Text", bs, "FullName");

            rbSex.DataBindings.Clear();
            rbSex.DataBindings.Add("Checked", bs, "Sex");

            if (rbSex.Checked == true)
            {
                rbFemale.Checked = true;
                rbMale.Checked = false;
            }
            else
            {
                rbFemale.Checked = false;
                rbMale.Checked = true;
            }

            txtDiscountPer.DataBindings.Clear();
            txtDiscountPer.DataBindings.Add("Text", bs, "DiscountPer");

            txtDebt.DataBindings.Clear();
            txtDebt.DataBindings.Add("Text", bs, "Debt");

            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", bs, "Address");

            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", bs, "Phone");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bs, "Email");

            txtState.DataBindings.Clear();
            txtState.DataBindings.Add("Text", bs, "State");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_CustomerDAO.GetAll();
        }

        public DataTable GetById(String cusId)
        {
            return m_CustomerDAO.GetById(cusId);
        }

        public DataTable GetByLikeId(String cusId)
        {
            return m_CustomerDAO.GetByLikeId(cusId);
        }

        public DataTable GetByName(String fullName)
        {
            return m_CustomerDAO.GetByName(fullName);
        }

        public DataTable GetByDiscount(double min, double max)
        {
            return m_CustomerDAO.GetByDiscount(min, max);
        }

        public DataTable GetByAddress(String address)
        {
            return m_CustomerDAO.GetByAddress(address);
        }

        public DataTable GetByPhone(String phone)
        {
            return m_CustomerDAO.GetByPhone(phone);
        }

        public DataTable GetByEmail(String email)
        {
            return m_CustomerDAO.GetByEmail(email);
        }

        public DataTable GetForReport()
        {
            return m_CustomerDAO.GetForReport();
        }

        public int Add(CustomerInfo info)
        {
            return m_CustomerDAO.Add(info);
        }

        public int Update(CustomerInfo info)
        {
            return m_CustomerDAO.Update(info);
        }

        public int UpdateSex(String cusId)
        {
            return m_CustomerDAO.UpdateSex(cusId);
        }

        public int Delete(String cusId)
        {
            return m_CustomerDAO.Delete(cusId);
        }

        public DataRow AddNewRow()
        {
            return m_CustomerDAO.AddNewRow();
        }

        public void AddNewCustomer(DataRow m_Row)
        {
            m_CustomerDAO.AddNewCustomer(m_Row);
        }

        public bool SaveData()
        {
            return m_CustomerDAO.SaveData();
        }
    }
}
